package org.sklse.learningassistant.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.sklse.learningassistant.bean.Plan;
import org.sklse.learningassistant.bean.Sign;
import org.sklse.learningassistant.bean.Wish;
import org.sklse.learningassistant.service.PlanService;
import org.sklse.learningassistant.repository.PlanRepository;
import org.sklse.learningassistant.repository.SignRepository;
import org.sklse.learningassistant.repository.WishRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Habit")
public class PlanController {

	@Autowired
	private PlanRepository planRepository;

	@Autowired
	private SignRepository signRepository;

	@Autowired
	private WishRepository wishRepository;
	@Resource
	private PlanService planService;
	
	@ResponseBody 
	  @RequestMapping("/getPlan")
	public List<Plan> getPlan(HttpServletResponse response){
		//服务器端跨域的配置,允许跨域
	    response.setHeader("Access-Control-Allow-Origin","*");  
		return planService.getPlan();
	}
	
	/*
	 * @ResponseBody
	 * 
	 * @RequestMapping("/addPlan") public void addPlan(HttpServletResponse
	 * response, Integer pID, String p_name, String description, Integer
	 * signed_num, Integer total_num) { //服务器端跨域的配置,允许跨域
	 * response.setHeader("Access-Control-Allow-Origin","*");
	 * planService.addPlan(pID, p_name, description, signed_num, total_num); }
	 */
	@ResponseBody 
	@RequestMapping(value = "/addPlan", method = RequestMethod.POST)
	public void addPlan(HttpServletResponse response, @RequestBody Plan plan) {
		//服务器端跨域的配置,允许跨域
	    response.setHeader("Access-Control-Allow-Origin","*");
	    System.out.println(plan.getpName());
		//planService.addPlan(plan.getpID(), plan.getpName(), plan.getDescription(), plan.getSignedNum(),
				//plan.getTotalNum(), plan.getBell(), plan.getwName(), plan.getHour(), plan.getMinute());
		planRepository.save(plan);
        //planService.addSign(plan.getpID(),plan.getTotalNum());
		int total = plan.getTotalNum();
		int id = plan.getpID();
		/*for (int i = 1; i <= total; i++) {
			// 自增长的主键可以不填
			Sign sign = new Sign(id, i, 0);
			signRepository.save(sign);
		}*/
		Wish wish = new Wish(plan.getpID(), plan.getwName());
        wishRepository.save(wish);

	}
	/*
	 * @ResponseBody
	 * 
	 * @RequestMapping("/updatePlan") public void updatePlan(HttpServletResponse
	 * response, String p_name, String description, Integer signed_num, Integer
	 * total_num, Integer pID) { //服务器端跨域的配置,允许跨域
	 * response.setHeader("Access-Control-Allow-Origin","*");
	 * planService.updatePlan(p_name, description, signed_num, total_num, pID);
	 * }
	 */
	@ResponseBody 
	@RequestMapping(value = "/updatePlan", method = RequestMethod.POST)
	public void updatePlan(HttpServletResponse response, @RequestBody Plan plan) {
		//服务器端跨域的配置,允许跨域
	    response.setHeader("Access-Control-Allow-Origin","*");  
		planService.updatePlan(plan.getpName(), plan.getDescription(), plan.getSignedNum(), plan.getTotalNum(),
				plan.getpID());
	}
	
	@ResponseBody
	@RequestMapping(value = "/deletePlan/{pID}", method = RequestMethod.DELETE)
	public void updatePlan(HttpServletResponse response, @PathVariable("pID") Integer pID) {
		// 服务器端跨域的配置,允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");
		planService.deletePlan(pID);
	}

	@ResponseBody
	@RequestMapping(value = "/sign/{pID}/{day}/{num}", method = RequestMethod.GET)
	public void sign(HttpServletResponse response, @PathVariable("pID") Integer pID, @PathVariable("day") Integer day,
			@PathVariable("num") Integer num) {
		// 服务器端跨域的配置,允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");
		planService.sign(pID, day, num);

	}

	@ResponseBody
	@RequestMapping(value = "/getSign/{pID}")
	public List<Sign> getSign(HttpServletResponse response, @PathVariable("pID") Integer pID) {
		// 服务器端跨域的配置,允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");
		return planService.getSign(pID);
	}

}
